#ifndef SERVER_H
#define SERVER_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <dirent.h>
#include <mysql/mysql.h>
#include <time.h>
#include <libgen.h>
#include <linux/limits.h>
#include <pthread.h>

#define MAX_CLIENTS 100
#define BUFFER_SIZE 4096
#define DB_HOST "localhost"
#define DB_USER "root"
#define DB_PASS "1"
#define DB_NAME "cloudfs_db"
#define SERVER_PORT 8080
#define USER_DIR_PREFIX "users/"
#define MAX_PATH_LEN 512
#define RED_TEXT "\033[31m"
#define YELLOW_TEXT "\033[33m"
#define BLUE_TEXT "\033[34m"
#define RESET_TEXT "\033[0m"

typedef enum {
    CONNECT,
    DISCONNECT,
    LOGIN,
    LOGOUT,
    UPLOAD,
    DOWNLOAD,
    DELETE,
    ERROR,
    SYSTEM,
    STARTUP,
    SHUTDOWN
} EventType;

extern MYSQL *conn; // 全局数据库连接

// 网络通信层
void handle_client(int client_sock);

// 用户认证模块
int authenticate_user(const char *username, const char *password);
int register_user(const char *username, const char *password);
void handle_logout(const char *username);

// 目录操作模块
int create_directory(int user_id, const char *dirname, int parent_id);
char* get_full_path(int user_id, int directory_id, const char *filename);

// 文件操作模块
void handle_upload(int sockfd, const char *username, const char *filename, long file_size, int directory_id);
void handle_download(int sockfd, const char *username, const char *filename, int directory_id);
void handle_list_files(int sockfd, const char *username, int directory_id);
void handle_delete_file(const char *username, const char *filename, int directory_id);

// 命令解析与响应
void send_response(int sockfd, const char *status, const char *message);
void parse_command(char *buffer, char **cmd, char **arg1, char **arg2, char **arg3);
char* my_hash_password(const char *password);  // 已重命名避免与MySQL库冲突

//日志模块
void log_event(const char *event_type, const char *username, const char *details);


#endif // SERVER_H